// EPropertyDate.cpp: implementation of the EPropertyDate class.
//
//////////////////////////////////////////////////////////////////////

#include "stdafx.h"
#include "others/EPropWndEdit.h"
#include "others/EPropertyDateWindow.h"
#include "IPropertyHost.h"
#include "EPropertyDate.h"

EPropertyDate::EPropertyDate(IPropertyHost *pHost, const std::wstring &sName, time_t *pProperty, bool bEnabled)
    : EIconTextButtonProperty(pHost, sName, pProperty, bEnabled)
    , m_pPropertyWindow(nullptr)
{}

EPropertyDate::~EPropertyDate()
{
    CloseMothCalCtrlWindow();
}

bool EPropertyDate::OnPropApply()
{
    bool bPropertyChanged = false;
    time_t oldt = GetPropertyValue();
    time_t newt = oldt;
    if(m_pPropertyWindow) {
        COleDateTime dt;
        m_pPropertyWindow->GetCurSel(dt);
        newt = Utils::OleDateTimeToTime(dt);
        CloseMothCalCtrlWindow();
    } else {
        ASSERT(m_bSelected == true);
        ASSERT(m_pEdit != NULL);
        ASSERT(IsEnabled() == true);

        wchar_t c[256];
        ::GetWindowTextW(m_pEdit->GetSafeHwnd(), c, 255);
        CStringW s(c);

        bool bFormatGood = false;

        int yy = 0;
        int mm = 0;
        int dd = 0;
        if(swscanf_s(s, L"%2d/%2d/%2d", &yy, &mm, &dd) == 3) {
            bFormatGood = true;
        } else if(swscanf_s(s, L"%4d/%2d/%2d", &yy, &mm, &dd) == 3) {
            bFormatGood = true;
        } else if(swscanf_s(s, L"%2d-%d-%d", &yy, &mm, &dd) == 3) {
            bFormatGood = true;
        } else if(swscanf_s(s, L"%4d-%2d-%2d", &yy, &mm, &dd) == 3) {
            bFormatGood = true;
        }

        if(bFormatGood) {
            if((1970 <= yy && yy <= 2099) && (1 <= mm && mm <= 12) && (1 <= dd && dd <= 31)) {
                newt = CTime(yy, mm, dd, 0, 0, 0).GetTime();
            }
        }
    }

    if(oldt != newt) {
        if(bPropertyChanged = m_pHost->PropertyChanging(m_pProperty, &newt)) {
            *(time_t *)m_pProperty = newt;
        }
    }

    return bPropertyChanged;
}

std::wstring EPropertyDate::GetTextValue()
{
    auto val = *((time_t *)m_pProperty);
    CTime t(val);
    CStringW s = t.Format(L"%Y/%m/%d");
    return s.GetString();
}

bool EPropertyDate::HasEdit()
{
    return true;
}

void EPropertyDate::CloseMothCalCtrlWindow()
{
    if(m_pPropertyWindow) {
        m_pPropertyWindow->DestroyWindow();
        m_pPropertyWindow.reset();
    }
}

time_t EPropertyDate::GetPropertyValue()
{
    return *(time_t *)m_pProperty;
}

bool EPropertyDate::HasButton()
{
    return true;
}

bool EPropertyDate::OnCommand(EPropWnd *pParent, WPARAM wParam, LPARAM lParam)
{
    bool bPropertyChanged = false;
    if(m_pPropertyWindow) {
        CloseMothCalCtrlWindow();
    } else {
        int nBeg = -1;
        int nEnd = -1;
        pParent->GetIDRange(nBeg, nEnd);

        CPoint ButtonCenter = m_ButtRect.BottomRight();
        CRect r(0, 0, 210, 170);
        r.OffsetRect(ButtonCenter.x - 210, ButtonCenter.y);

        m_pPropertyWindow = std::make_unique<EPropertyDateWindow>(pParent);
        m_pPropertyWindow->Create(WS_TABSTOP | WS_CHILD | WS_VISIBLE | WS_BORDER | MCS_DAYSTATE, r, pParent, nBeg + 1);
        m_pPropertyWindow->SetCurSel(COleDateTime(GetPropertyValue()));
    }

    return bPropertyChanged;
}

void EPropertyDate::OnPropClose()
{
    EIconTextButtonProperty::OnPropClose();
    CloseMothCalCtrlWindow();
}